package chase.myrewards;

import java.io.*;
import java.sql.DriverManager;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.logging.Logger;

import javax.servlet.*;
import javax.servlet.http.*;

import com.google.appengine.api.rdbms.AppEngineDriver;
import com.google.appengine.api.utils.SystemProperty;
import com.google.cloud.sql.jdbc.Connection;

import chase.myrewards.beans.*;

/**
 * Servlet that takes customer id and account number as input and 
 * gets all the points usage of the account and puts it in a bean.
 * 
 * @author Meena Abdou
 * @date April 24, 2013
 */

public class RewardsUsageServlet extends HttpServlet implements Serializable {

	/**
	 * This number must be unique for every class. And it can never change.
	 * You have to generate it automatically by clicking on the class name and
	 * then hitting Ctrl+1.  Then click on Add generated serial version ID. Then
	 * copy these comments on top of the generated id.
	 * 
	 * @serial serialVersionUID
	 */
	private static final long serialVersionUID = 4039483708069373486L;
	private static final Logger log = Logger.getLogger(AccountListServlet.class.getName());
	private Customer cust;
	private Account account;
	private RewardsUsageBean ru;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		log.info("Session Id is " + request.getSession().getId());
		
		HttpSession session = request.getSession();
		String address;
		address = "/WEB-INF/jsp/rewardsusage.jsp";
		cust = null;
		account = null;
		ru = new RewardsUsageBean();

		// Added by Archi
		// Making connection with the database
		// Getting breakdown of used points for a account number

		if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) {
			Connection c = null;
			try {
				DriverManager.registerDriver(new AppEngineDriver());
				c = (Connection) DriverManager.getConnection("jdbc:google:rdbms://myrewards2013:myrewardsdatabase/myrewards");

				// Get parameters from session objects
				cust = (Customer) session.getAttribute("customer");
				account = (Account) request.getSession().getAttribute("accountinfo");
				String id = cust.getCustomerID();
				String num = account.getAccountNum();
				log.info("Account num " + account.getAccountNum());
				
				// Getting used points, usage date and description for the selected account number from used points table			
				java.sql.Statement stmt =  c.createStatement();
				java.sql.ResultSet rs = stmt.executeQuery(Queries.REWARDS_USAGE(num, id));
				//
				rs.next();					

				do {
					ru.insertEntry(rs.getInt("used_points"), rs.getDate("date"), rs.getString("description"));
									
				}while(rs.next()); // End of while

				session.setAttribute("rewardsusage", ru);

				RequestDispatcher dispatcher = request.getRequestDispatcher(address);
				dispatcher.forward(request, response);
			} 
			catch (SQLException e) {
				e.printStackTrace();
			} 
			finally {
				if (c != null){ 
					try {
						c.close();
					} 
					catch (SQLException ignore) {}
				}
			} // End of try-catch block
		}

		else{
			session.setAttribute("rewardsusage", ru);

			RequestDispatcher dispatcher = request.getRequestDispatcher(address);
			dispatcher.forward(request, response);
		}
		
		return;
	}
}
